package com.ailk.cuc.autotestbg.xml.business.impl.ecs;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;

import com.ailk.cuc.autotestbg.common.bean.AutoCaseResultBGBean;
import com.ailk.cuc.autotestbg.common.bean.AutoCaseStepBGBean;
import com.ailk.cuc.autotestbg.common.bean.AutoPlanResultBGBean;
import com.ailk.cuc.autotestbg.common.bean.AutoTestCaseBean;
import com.ailk.cuc.autotestbg.common.bean.AutoTestPlanBean;
import com.ailk.cuc.autotestbg.common.bean.AutoTestPlanTestCaseBean;
import com.ailk.cuc.autotestbg.common.business.interf.BusinessProcessInterface;
import com.ailk.cuc.autotestbg.common.business.report.BusinessReport;
import com.ailk.cuc.autotestbg.common.exception.DataBaseException;
import com.ailk.cuc.autotestbg.common.utils.LogUtils;
import com.ailk.cuc.autotestbg.common.utils.SpringContextUtil;
import com.ailk.cuc.autotestbg.xml.bean.AutoEssDataBean;
import com.ailk.cuc.autotestbg.xml.constants.EssEcsConstants;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.minilcu.LcuEngine;
import com.minilcu.exception.ExecCallerException;

public class EcsProcesserImpl implements BusinessProcessInterface {

	private static Log logger = LogUtils.getLogger(EcsProcesserImpl.class.getName());
	public void processBusi(Map<String, Object> argMap) {
		
		AutoTestPlanBean planBean = (AutoTestPlanBean)argMap.get("planBean");
		SqlMapClient sqlMapClient = (SqlMapClient) SpringContextUtil.getBean("sqlMapClient");
		boolean planStatus = true;
		
		try {
			//执行测试计划，写入的结果表步骤如下
			//1.插入计划结果表
			AutoPlanResultBGBean planResultBGBean = new AutoPlanResultBGBean();
			insertPlanResultBGBean(planResultBGBean, planBean, sqlMapClient);
			
			ArrayList<AutoTestPlanTestCaseBean> testPlanTestCaseList = planBean.getPlanCaseList();
			
			for(AutoTestPlanTestCaseBean planCaseBean : testPlanTestCaseList){
				AutoTestCaseBean testCaseBean = planCaseBean.getTestCaseBean();
				
				//2.写入用例结果表
				AutoCaseResultBGBean caseResultBGBean = new AutoCaseResultBGBean();
				insertCaseResultBGBean(caseResultBGBean, testCaseBean, planCaseBean, planBean, sqlMapClient);
				argMap.put("caseResultBGBean", caseResultBGBean);
				
				/**以下是初始化AutoEssDataBean，找到testCaseBean的第一步 start*/
				ArrayList<AutoCaseStepBGBean> caseStepBGList = testCaseBean.getCaseStepList();
				AutoCaseStepBGBean caseStepBean = caseStepBGList.get(0);
				
				AutoEssDataBean ecsDataBean = null;
				try {
					String dataId = caseStepBean.getInputDataId()+"";
					ecsDataBean = (AutoEssDataBean)sqlMapClient.queryForObject("xml_ess_ecs_assembly.getEssDatabean", dataId);
				} catch (SQLException e) {
					logger.error("ECS查询essData操作异常");
					logger.error(e.getMessage());
					throw new DataBaseException("ECS查询essData操作异常");
				}
				
				argMap.put("ecsDataBean", ecsDataBean);
				argMap.put("caseStepBean", caseStepBean);
				/**以上是初始化AutoEssDataBean，找到testCaseBean的第一步 end*/
				
				//TODO 此处调用LCU工具
				HashMap<String,Object> outParamMap = new HashMap<String,Object>();
				HashMap<String,Object> inParamMap = (HashMap<String,Object>)argMap;
				
				//流程配置文件的key值
				String flowKey = "1".equals(argMap.get("interfaceType").toString())?EssEcsConstants.XML_BUSI_PROCESSER_TYPE_ESS:EssEcsConstants.XML_BUSI_PROCESSER_TYPE_ECS +"_"+planBean.getPlanId();
				logger.debug("flowKey : "+flowKey);
				LcuEngine lcuEngine = (LcuEngine)SpringContextUtil.getBean("lcuEngine");
				lcuEngine.startEgnine(flowKey, inParamMap, outParamMap);
				
				//3.更新caseResultBGBean
				caseResultBGBean = getCaseResultBGBean(caseResultBGBean, sqlMapClient);
				if(caseResultBGBean.getTotalStepNum() == caseResultBGBean.getOverStepNum()){

					planResultBGBean.setOverCaseNum(planResultBGBean.getOverCaseNum()+1);

					if(getCaseResultIsRight(caseResultBGBean, sqlMapClient)){
						caseResultBGBean.setCurrStatus(1);
					}else{
						caseResultBGBean.setCurrStatus(3);
						planStatus = false;
					}
				}
				updateCaseResultBGBean(caseResultBGBean, sqlMapClient);
				
				//4.更新planResultBGBean
				if(planResultBGBean.getOverCaseNum() == planResultBGBean.getTotalCaseNum()){
					if(planStatus){
						planResultBGBean.setCurrStatus(1);
					}else{
						planResultBGBean.setCurrStatus(3);
					}
				}
				updatePlanResultBGBean(planResultBGBean, sqlMapClient);
			}
		}catch (DataBaseException e) {
			logger.error("ESS数据库操作异常");
			logger.error(e.getMessage());
			throw new RuntimeException();
		} catch (ExecCallerException e) {
			logger.error("LCU工具抛出异常");
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
		
		logger.info("开始生成测试报告");
		try {
			BusinessReport businessReport = new BusinessReport();
			businessReport.createReport(planBean);
		} catch (Exception e) {
			logger.error("生成测试报告发生异常");
			logger.error(e.getMessage());
			throw new RuntimeException();
		}
		logger.info("生成测试报告完毕");
		
	}
	
	public void insertPlanResultBGBean(AutoPlanResultBGBean planResultBGbean,AutoTestPlanBean planBean,SqlMapClient sqlMapClient) throws DataBaseException{

		planResultBGbean.setTaskId(planBean.getTaskId());
		planResultBGbean.setTestPlanId(planBean.getPlanId());
		planResultBGbean.setTotalCaseNum(planBean.getPlanCaseList().size());
		planResultBGbean.setOverCaseNum(0);
		planResultBGbean.setCurrStatus(2);
		planResultBGbean.setTestPlanName(planBean.getPlanName());
		planResultBGbean.setStatusDesc("");

		try {
			sqlMapClient.insert("common_result.insertPlanResultBGBean",planResultBGbean);
		} catch (SQLException e) {
			logger.error("EcsProcesserImpl.insertPlanResultBGBean方法异常");
			logger.error(e.getMessage());
			throw new DataBaseException("ECS处理插入PlanResult数据操作异常");
		}
	}

	public void insertCaseResultBGBean(AutoCaseResultBGBean caseResultBGBean,AutoTestCaseBean testCaseBean,AutoTestPlanTestCaseBean planCaseBean,AutoTestPlanBean planBean,SqlMapClient sqlMapClient) throws DataBaseException{
		
		caseResultBGBean.setTaskId(planBean.getTaskId());
		caseResultBGBean.setPlanId(planBean.getPlanId());
		caseResultBGBean.setTestCaseId(planCaseBean.getTestCaseId());
		caseResultBGBean.setTestNo(planCaseBean.getTestNo());
		caseResultBGBean.setTotalStepNum(testCaseBean.getCaseStepList().size());
		caseResultBGBean.setOverStepNum(0);
		caseResultBGBean.setCurrStatus(2);
		caseResultBGBean.setTestCaseName(testCaseBean.getTestCaseName());
		caseResultBGBean.setStatusDesc("");
		
		try {
			sqlMapClient.insert("common_result.insertCaseResultBGBean",caseResultBGBean);
		} catch (SQLException e) {
			logger.error("EcsProcesserImpl.insertCaseResultBGBean方法异常");
			throw new DataBaseException("ECS处理插入caseResult数据操作异常");
		}
	}
	
	public void updatePlanResultBGBean(AutoPlanResultBGBean planResultBGBean,SqlMapClient sqlMapClient) throws DataBaseException{
		
		try {
			sqlMapClient.update("common_result.updatePlanResultBGBean",planResultBGBean);
		} catch (SQLException e) {
			logger.error("EcsProcesserImpl.updatePlanResultBGBean方法异常");
			throw new DataBaseException("ECS处理更新PlanResult数据操作异常");
		}
	}
	
	public AutoCaseResultBGBean getCaseResultBGBean(AutoCaseResultBGBean caseResultBGBean,SqlMapClient sqlMapClient) throws DataBaseException{
		
		try {
			return  (AutoCaseResultBGBean) sqlMapClient.queryForObject("common_result.getCaseResultBGBean", caseResultBGBean);
		} catch (SQLException e) {
			logger.error("EcsProcesserImpl.getCaseResultBGBean方法异常");
			throw new DataBaseException("ECS处理查询CaseResult操作异常");
		}
	}
	
	public boolean getCaseResultIsRight(AutoCaseResultBGBean caseResultBGBean,SqlMapClient sqlMapClient) throws DataBaseException{
		int result = -1;
		
		try {
			result = (Integer)sqlMapClient.queryForObject("common_result.getCaseResultIsRight", caseResultBGBean);
		} catch (SQLException e) {
			logger.error("EcsProcesserImpl.getCaseResultIsRight方法异常");
			throw new DataBaseException("ECS处理查看CaseResultIsRight操作异常");
		}
		
		if(result != 0){
			return false;
		}else{
			return true;
		}
	}
	
	public void updateCaseResultBGBean(AutoCaseResultBGBean caseResultBGBean,SqlMapClient sqlMapClient) throws DataBaseException{
		
		try {
			sqlMapClient.update("common_result.updateCaseResultBGBean",caseResultBGBean);
		} catch (SQLException e) {
			logger.error("EcsProcesserImpl.updateCaseResultBGBean方法异常");
			throw new DataBaseException("ECS处理更新CaseResult数据操作异常");
		}
	}
}
